/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.sys.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.sys.api.model.SysRole
import com.gitee.minimalismstyle.fresh.sys.dao.SysUserDao
import com.gitee.minimalismstyle.fresh.sys.api.model.SysUser
import com.gitee.minimalismstyle.fresh.sys.api.model.SysUserRoles
import com.gitee.minimalismstyle.fresh.sys.api.service.SysUserService
import com.gitee.minimalismstyle.fresh.sys.dao.SysUserRoleDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Service

/**
 * 用户业务实现
 * @author maochao
*/
@Primary
@Service
class SysUserServiceImpl: SysUserService, LongModelBaseServiceImpl<SysUserDao, SysUser>() {

    @Autowired
    private lateinit var sysUserRoleDao: SysUserRoleDao

    override fun findUserRolesByUsername(username: String): R<SysUserRoles> {
        val sysUserRoles: SysUserRoles? = dao.findByUsername(username)
        return if(sysUserRoles != null){
            val roles = sysUserRoleDao.findRoleByUserId(sysUserRoles.id!!)
            sysUserRoles.roles = roles?.toMutableList()
            R.success(sysUserRoles)
        }else{
            R.error("找不到相关用户")
        }

    }

    override fun getPetnameByIds(vararg ids: Long): R<Map<Long, String>> {
        val users = dao.getPetnameByIds(ids)
        val petnameMap = hashMapOf<Long, String>()
        users.forEach { petnameMap[it.id!!] = it.petname!! }
        return R.success(petnameMap)
    }
}